load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
load("@aspect_rules_ts//ts:defs.bzl", rules_js_tsconfig = "ts_config")
load("//tools:defaults.bzl", "jasmine_test", "npm_package", "ts_project")

package(default_visibility = ["//visibility:public"])

npm_link_package(
    name = "node_modules/@angular/cdk/schematics",
    src = "//src/cdk/schematics:npm_package",
    root_package = package_name(),
)

copy_to_bin(
    name = "collection_assets",
    srcs = [
        "collection.json",
        "migration.json",
    ],
)

copy_to_bin(
    name = "ng_generate_assets",
    srcs = glob([
        "ng-generate/*/files/**/*",
    ]),
)

copy_to_bin(
    name = "schema_assets",
    srcs = glob([
        "**/schema.json",
    ]),
)

copy_to_bin(
    name = "package_json",
    srcs = ["package.json"],
)

rules_js_tsconfig(
    name = "tsconfig",
    src = "tsconfig.json",
    deps = [
        "//:node_modules/@types/node",
    ],
)

rules_js_tsconfig(
    name = "tsconfig-test",
    src = "tsconfig-test.json",
    deps = [
        ":tsconfig",
        "//:node_modules/@types/jasmine",
    ],
)

ts_project(
    name = "schematics",
    srcs = glob(
        ["**/*.ts"],
        exclude = [
            "**/*.spec.ts",
            "paths.ts",
            "ng-generate/*/files/**/*.ts",
        ],
    ),
    tsconfig = ":tsconfig",
    deps = [
        ":node_modules/@angular/cdk/schematics",
        "//:node_modules/@angular-devkit/core",
        "//:node_modules/@angular-devkit/schematics",
        "//:node_modules/@schematics/angular",
        "//:node_modules/rxjs",
        "//:node_modules/tslint",
        "//:node_modules/typescript",
    ],
)

ts_project(
    name = "paths",
    testonly = True,
    srcs = ["paths.ts"],
    data = [
        ":collection_assets",
    ],
    tsconfig = ":tsconfig",
    deps = [
        "//:node_modules/@bazel/runfiles",
    ],
)

# This package is intended to be combined into the main @angular/material package as a dep.
npm_package(
    name = "npm_package",
    srcs = [
        "package.json",
        ":collection_assets",
        ":ng_generate_assets",
        ":schema_assets",
        ":schematics",
        "//src/material/schematics/ng-generate/theme-color:npm_package_assets",
        "//src/material/schematics/ng-update:ng_update_index",
    ],
)

ts_project(
    name = "unit_test_sources",
    testonly = True,
    srcs = glob(
        ["**/*.spec.ts"],
        exclude = [
            "**/*.e2e.spec.ts",
            "ng-generate/*/files/**/*.spec.ts",
        ],
    ),
    tsconfig = ":tsconfig-test",
    deps = [
        ":node_modules/@angular/cdk/schematics",
        ":paths",
        ":schematics",
        "//:node_modules/@angular-devkit/core",
        "//:node_modules/@angular-devkit/schematics",
        "//:node_modules/@schematics/angular",
        "//:node_modules/@types/fs-extra",
        "//:node_modules/@types/jasmine",
        "//:node_modules/@types/node",
        "//:node_modules/fs-extra",
        "//:node_modules/parse5",
        "//src/cdk/schematics/testing",
    ],
)

jasmine_test(
    name = "unit_tests",
    data = [
        ":collection_assets",
        ":ng_generate_assets",
        ":schema_assets",
        ":schematics",
        ":unit_test_sources",
        "//src/material/schematics:package_json",
    ],
)
